﻿
#ifndef MODBUSRTULINK_H
#define MODBUSRTULINK_H


#include <QObject>
#include <QModbusRtuSerialMaster>
#include <QModbusClient>
#include <QModbusDevice>
#include <QModbusDataUnit>
#include <QtSerialPort/QSerialPortInfo>
#include <QtSerialPort/QSerialPort>
#include <QFile>
#include <QTime>
#include <QMutex>

#include "Stdafx.h"
#include "Linkinterface.h"

class ModbusRtuLink : public LinkInterface
{
    Q_OBJECT
    Q_PROPERTY_AUTO(int     , baud)
    Q_PROPERTY_AUTO(int     , stopBits)
    Q_PROPERTY_AUTO(int     , dataBits)
    Q_PROPERTY_AUTO(int     , parity)
    Q_PROPERTY_AUTO(QString , portName)
public:
    explicit ModbusRtuLink(const QString & port_name,
                        int baud ,int data_bits ,
                        int stop_bits ,int parity, QObject *parent = nullptr);
    Q_INVOKABLE bool  connect      ()override;
    Q_INVOKABLE void  disConnect   ()override;
    Q_INVOKABLE bool  isConnected  ()override;
public:
    Q_INVOKABLE void writeUnit(QModbusDataUnit::RegisterType type ,
                               int serverAddress,
                               int startAddress,
                               QVector<quint16>values);

    Q_INVOKABLE void readUnit(QModbusDataUnit::RegisterType type,
                              int serverAddress,
                              int startAddress,
                              int readNum);

signals:
    void readUnitFinished();
    void writeUnitFinished();
    void readData(int serverAddress, int startAddress,const QModbusDataUnit &unit);
    void writeData(int serverAddress, int startAddress, const QModbusDataUnit &unit);
private:
    QModbusRtuSerialMaster *port_{nullptr};
    QMutex mutex_;
    bool   isConnected_{false};
};

#endif // MODBUSRTULINK_H
